<template>
    <h2>修改投资项目信息</h2>
    <table  class="table-bordered" border="1">
       <tr>
           <td>投资项目名称</td>
           <td>
               <input type="text" placeholder="请输入投资项目名称" v-model="info.I_Name"/>
           </td>
       </tr>
       <tr>
           <td>总投资金额</td>
           <td>
               <input type="text"  placeholder="请输入总投资金额" v-model="info.I_Budget"/>
           </td>
       </tr>
       <tr>
           <td colspan="2" style="text-align: center;">
              <input type="button" value="修改"  class="btn btn-success" @click="UpdateData" />
           </td>
       </tr>
    </table>
</template>

<script setup lang="ts">
import { useRoute,useRouter } from 'vue-router';
import {ref,onMounted} from 'vue';
import axios from 'axios';

const route = useRoute();

const router = useRouter();
//创建响应式数据
const info = ref({
    I_Name:"",
    I_Budget:"",
})

//生命周期函数勾
onMounted(()=>{
    LoadInfo();
})

const LoadInfo = ()=>{
      axios({
         url:"https://localhost:44337/api/Invests/Backfill",
         method:"get",
         params:{
             Iid:route.params.Iid
         }
      }).then(res=>{
          info.value.I_Name = res.data.I_Name;
          info.value.I_Budget = res.data.I_Budget;
          console.log(info.value);
      }).catch(err=>{
          console.log(err);
      })
}

const UpdateData =() =>{
      axios({
          url:"https://localhost:44337/api/Invests/EditeInvestInfo",
          method:"put",
          data:{
            I_Name:info.value.I_Name,
            I_Budget:info.value.I_Budget,
            I_id:route.params.Iid
          }
      }).then(res=>{
          if(res.data>0)
          {
              alert("修改成功");
              router.push("/ShowInvestInfo");
          }else if(res.data==-1)
          {
              alert("无法修改,已有人投资");
          } else{
             alert('修改失败')
          }
          console.log(res.data);
      }).catch(err=>{
          console.log(err);
      })
}
//alert(route.params.Iid)
</script>